require(["../../conf/config.js"],function(){
	require(["jquery","common","ajaxMapping","template"],function($,common,ajaxMapping,template){
		
		//判断洋码头的登录状态
		let loginStatus = 0;
		let userObj = null;
		var cookiestr = getCookie("userlist");
		var userArray = JSON.parse(cookiestr);
		(function judge(){
			userArray.forEach(function(item){
				if(item.status == 1){
					loginStatus = 1;
					userObj = item;	
					// console.log(userObj);
					$(".username").html(userObj.username);
					$(".login").css("display","none");
					$(".register").css("display","none");
				}
			})
		})();
		
		
		//鼠标划入用户名
		var username = document.getElementsByClassName("username")[0];
		$(".username").on("mouseenter",function(){
			var span = document.createElement("span");
			span.innerText = "退出登录"
			span.className = "tuichu";
			username.appendChild(span);
			// console.log($(".tuichu"));
			//*****鼠标点击退出登录*********
			span.addEventListener("click",function(){
				// console.log("aaa");
				userArray.forEach(function(item){
						item.status = 0;		
				})
				setCookie("userlist", JSON.stringify(userArray), 20);
				loginStatus = 0;
				window.location.replace("http://localhost:8000/pages/index/index.html")
			})
		})
		
		//鼠标划出用户名
		$(".username").on("mouseleave",function(){
			var tuichu = document.getElementsByClassName("tuichu")[0];
			tuichu.remove();
		})
		//点击回到顶部
		$(".backTop span").click(function(){
			$("html").animate({scrollTop:0},300);
		})
		
		//获取路径id
		var ProdId = null;
		function getParam(){
			var C1=window.location.href.split("?")[1]; //得到id
			ProdId =C1.split("=")[1]; //得到id内容
		};
		getParam();
			
		//商品信息
		function cartAjax(resolve,reject){
			$.ajax({
				url : "../index/guessData.json",
				dataType : "json",
				type: "GET",
				success : function(data){
					var weNeed = data;
					// console.log(ProdId);
					var obj = weNeed.find(function(item){
						return item.ProdId == ProdId;
					})
					$("#product").load("cart.html",function(){
						var htmlstr = template('cart',{list:obj});
						// ????用jquery的html()方法会把script标签也读取出来
						$("#product").html(htmlstr);
					})
					
				}
			})
			// resolve();
			
		};
		cartAjax();
		//提交购物车
		function submitCart(){
			var carttitle = document.getElementsByTagName("h3")[0].innerText;
			var cartprice = document.getElementsByClassName("price")[0].innerText;
			var cartnumber = document.getElementsByTagName("input")[1].value;	
			var cart = null;
			var cookiestr = getCookie("userlist");
			var arr = JSON.parse(cookiestr);
			if(!cartnumber){
				cartnumber = 1;
			}
			cart = {carttitle,cartprice,cartnumber}
			userObj.shopcar.push(cart);
			console.log(userObj);	
			arr.forEach(function(item){
				console.log(item);
				if(Number(item.phone) == Number(userObj.phone)) {
					item.shopcar = userObj.shopcar;
				}
			})
			console.log(arr);
			setCookie("userlist", JSON.stringify(arr), 20);	
		}
		
		$(".shopCar").click(function(){
			if(loginStatus==0){
				window.location.replace("http://localhost:8000/pages/login/login.html");
			}else{
				window.location.replace("http://localhost:8000/pages/shopcar/shopcar.html");
			}
		})
		
		// new Promise(cartAjax)
		// .then(
		// 	clickEvent
		// );
		// window.onload = function{
		// 	$("#add_cart").on("click",function(){
		// 	console.log("加入购物车成功！");
		// 	submitCart();	
		// 	});
		// 	$(".zhong span").eq(1).click = function(){
		// 		console.log("+++");
		// 		$("input[type='text']").val()++;
		// 	};
		// 	$(".zhong span").eq(0).click = function(){
		// 		console.log("--")
		// 		if($("input[type='text']").val()>1){
		// 			$("input[type='text']").val()--;
		// 		}
		// 	};
		// }
		setTimeout(function(){
			var proCount = document.getElementsByTagName("input")[1];
			proCount.value = 1;
			$("#add_cart").click(function(){
				console.log("加入购物车成功！");
				alert("成功添加至购物车！")
				submitCart();
			});
			//商品数量加
			$(".zhong span").eq(1).click(function(){
				console.log("+++");
				proCount.value++;
			});
			//商品数量减
			$(".zhong span").eq(0).click(function(){
				console.log("---");
				if(proCount.value > 1){
					proCount.value--;
				};
			});
		},500);
		
		//进入购物车
		$("#goShopCar").click(function(){
			if(loginStatus=0){
				window.location.replace("http://localhost:8000/pages/login/login.html");
			}else{
				window.location.replace("http://localhost:8000/pages/shopcar/shopcar.html");
			}
		})
		
		//放大镜效果
		setTimeout(function(){
			//鼠标进入
			$("#fatherPic").mousemove(function(e){
				var e = e || event;
				$("#loupe").css("display","block");
				var mouse = {
					x : e.offsetX,
					y : e.offsetY
				};
				console.log(mouse);
				console.log(document.getElementById("loupe").children[0]);
				document.getElementById("loupe").children[0].style.marginLeft =200 +  (-2*mouse.x) + "px";
				document.getElementById("loupe").children[0].style.marginTop =200 +   (-2*mouse.y) + "px";
			});
			
			//鼠标离开
			$("#fatherPic").on("mouseleave",function(e){
				$("#loupe").css("display","none");
			})
		},50)
		
//***********************************************************************	
	})
})